﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant
{
    public partial class frmQLNhaHang : Form
    {
        NhaHangService nhService = new NhaHangService();
        public frmQLNhaHang()
        {
            InitializeComponent();
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            frmNhaHang frm = new frmNhaHang();
            frm.ShowDialog();
            LoadNH();
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        bool fix = false;
        private void LoadNH()
        {
            DataTable dbtab = new DataTable();
            if (fix)
                dbtab = nhService.LoadNHDrityread();
            else
                dbtab = nhService.LoadNHDrityreadError();
            lv_NH.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                lv_NH.Items.Add(liv);
            }
        }

        private void frmQLNhaHang_Load(object sender, EventArgs e)
        {
            if (fixloi.Checked)
                fix = true;
            LoadTatCaNH();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadNH();
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            int index = lv_NH.FocusedItem.Index;
            if (index >= 0)
            {

                int ma = int.Parse(lv_NH.Items[index].Text);
                NhaHang nh = nhService.TimNhaHang(ma);
                frmNhaHang frm = new frmNhaHang();
                frm.Nh = nh;
                frm.flag = 1;
                frm.fix = fix;
                frm.ShowDialog();
                LoadTatCaNH();
            }
        }

        private void LoadTatCaNH()
        {
            DataTable dbtab = nhService.LoadNH();
            lv_NH.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                lv_NH.Items.Add(liv);
            }
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            int index = lv_NH.FocusedItem.Index;
            if (index >= 0)
            {
                int ma = int.Parse(lv_NH.Items[index].Text);
                if (nhService.XoaNH(ma))
                {
                    LoadNH();
                    MessageBox.Show("Bạn đã xóa nhà hàng thành công !", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                MessageBox.Show("Xóa nhà hàng công !", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void fixloi_CheckedChanged(object sender, EventArgs e)
        {
            if (fixloi.Checked)
                fix = true;
            else
                fix = false;
        }
    }
}
